using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using Microsoft.SharePoint.WebControls;

namespace PS.WebParts.Rollup.Core
{
    class DataSourceDictionary
    {
        Dictionary<string, DataTable> dataSources;
        DataSourceCollection dataSourceCollection;
        DataSet dataSet;

        public DataSourceDictionary(Dictionary<string, DataTable> dataSources)
        {
            this.dataSourceCollection = null;
            this.dataSet = new DataSet();
            this.dataSources = dataSources;
        }

        public DataSourceDictionary(DataSourceCollection dataSourceCollection)
        {
            this.dataSourceCollection = dataSourceCollection;
            this.dataSet = new DataSet();
            dataSources = new Dictionary<string, DataTable>(dataSourceCollection.Count);
        }

        public DataTable this[string dataSourceId]
        {
            get
            {
                DataTable source = null;

                if (dataSources.ContainsKey(dataSourceId))
                    source = dataSources[dataSourceId];

                if (source == null)
                {
                    source = UnwrapDataSource(dataSourceId);
                }
                return source;
            }
        }

        //public DataTable GetData(string dataSourceId, DataSet ds)
        //{
        //    DataTable source = null;

        //    if (dataSources.ContainsKey(dataSourceId))
        //        source = dataSources[dataSourceId];

        //    if (source == null)
        //    {
        //        source = UnwrapDataSource(dataSourceId, ds);
        //    }
        //    return source;
        //}

        private DataTable UnwrapDataSource(string key)
        {
            IDataSource source = GetDataSource(key);
            
            if (source == null)
                throw new DataSourceNotFoundException(key);
            
            ATabularDataSource ds = source as ATabularDataSource;

            if (ds == null)
                throw new DataSourceNotSupportedException(source.GetType());

            DataSourceView view = source.GetView(string.Empty);

            ATabularDataSourceView adw = (ATabularDataSourceView)view;
            DataTable data = adw.GetData();
            string id = ds.ID;
            data.TableName = id;

            if (dataSet != null)
            {
                if (data.DataSet == null)
                {
                    dataSet.Tables.Add(data);
                }
                else
                {
                    data = JoinProcessor.Union(data);
                    dataSet.Tables.Add(data); // = data.DataSet; // .Tables.Add(data);
                }
            }

            dataSources.Add(id, data);
            
            return data;
        }

        public ATabularDataSource GetDataSource(string key)
        {
            if (dataSourceCollection == null)
                throw new ApplicationException("DataSourcesCollection object has not been specified");
            
            foreach(DataSourceControl c in dataSourceCollection)
            {
                if (c.ID == key)
                    return (ATabularDataSource)c;
            }
                
            return null;                
        }

    
    }

    public class DataSourceNotFoundException : ApplicationException
    {
        public DataSourceNotFoundException(string id) : base("Datasource id not found :" + id)
        {
        }
    }
}
